
package ${package}.impl;

import ${package}.com.be.beadmin.device.entity.${className};
import ${package}.com.be.beadmin.device.entity.${className}_;
import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.com.be.beadmin.device.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import lombok.RequiredArgsConstructor;
import ${package}.mapper.${className}MapperEx;
import ${package}.repository.${className}Repository;
import ${package}.service.${className}Service;
import ${package}.dto.input.${className}Dto;
import ${package}.dto.output.${className}Ex;
import ${package}.dto.input.${className}QueryCriteria;
import ${package}.mapper.${className}Mapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Order;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
* @description 服务实现
* @author ${author}
* @date ${date}
**/
@Service
@RequiredArgsConstructor
public class ${className}ServiceImpl implements ${className}Service {

    @Autowired
    private  ${className}Repository ${changeClassName}Repository;
    @Autowired
    private  ${className}Mapper ${changeClassName}Mapper;
    @Autowired
    private  ${className}MapperEx ${changeClassName}ExMapper;

    @Override
    public Page<${className}Ex> get${className}ByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<${className}> page = ${changeClassName}Repository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(${className}_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(${className}_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<${className}Ex> page1 = page.map(${changeClassName}ExMapper::toEntity);
            return page1;

    }

    @Override
    public void add${className}(${className}Dto ${changeClassName}Dto) {
            ${className} result = ${changeClassName}Repository.find${className}ByIdAndDelete(${changeClassName}Dto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("${className}不存在,请重新查询");
            }
            ${className} ${changeClassName} = ${changeClassName}Mapper.toEntity(${changeClassName}Dto);
            ${changeClassName}Repository.save(${changeClassName});
    }

    @Override
    public void update${className}(${className}Dto ${changeClassName}Dto) {
            ${className} ${changeClassName} = ${changeClassName}Repository.find${className}ById(${changeClassName}Dto.getId());
            ${changeClassName} = ${changeClassName}Mapper.toEntity(${changeClassName}Dto);
            ${changeClassName}Repository.save(${changeClassName});
    }


    @Override
    public void del${className}(String id) {
        ${className} ${changeClassName} = ${changeClassName}Repository.find${className}ById(id);
        ${changeClassName}.setDelete(1);
        ${changeClassName}Repository.save(${changeClassName});
    }

    @Override
    public void del${className}s (List<String> ids) { }
}